﻿Public Class frmPrincipal

    Public frmGestionSolicitudPrestamo As frmGestionSolicitudPrestamo
    Public frmGestionClientes As frmGestionClientes
    Public frmGestionMarcasVehiculos As frmGestionMarcasVehiculos
    Public frmGestionUsuarios As frmGestionUsuarios
    Public frmGestionPrestamo As frmGestionPrestamo
    Public frmGestionCobranzaCuotas As frmGestionCobranzaCuotas
    Public frmProcesos As frmProcesos
    Public frmInformesEstadisticos As frmInformesEstadisticos
    Public frmListadoPrestamos As frmListadoPrestamos
    Public frmSesion As frmSesion
    Public frmAcercaDe As frmAcercaDe
    Public frmPassword As frmPassword

    Public manager As Boolean
    Public sesionIniciada As Boolean
    Public idUsuarioLogueado As String
    Public nombreUsuarioLogueado As String

    Dim utilidades As Utilidades

    Private Sub GestiónDeSolicitudesDePréstamoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GestiónDeSolicitudesDePréstamoToolStripMenuItem.Click
        Me.frmGestionSolicitudPrestamo = New frmGestionSolicitudPrestamo(Me)
        Me.frmGestionSolicitudPrestamo.ShowDialog()
    End Sub

    Private Sub SalirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalirToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub GestiónClientesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GestiónClientesToolStripMenuItem.Click
        Me.frmGestionClientes = New frmGestionClientes(Me)
        frmGestionClientes.ShowDialog()
    End Sub

    Private Sub GestiónDeUsuariosToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GestiónDeUsuariosToolStripMenuItem.Click
        Me.frmGestionUsuarios = New frmGestionUsuarios(Me)
        frmGestionUsuarios.ShowDialog()
    End Sub

    Private Sub GestiónDeMarcasDeVehículosToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GestiónDeMarcasDeVehículosToolStripMenuItem.Click
        Me.frmGestionMarcasVehiculos = New frmGestionMarcasVehiculos(Me)
        frmGestionMarcasVehiculos.ShowDialog()
    End Sub

    Private Sub ConsultaDePréstamosToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConsultaDePréstamosToolStripMenuItem.Click
        Me.frmGestionPrestamo = New frmGestionPrestamo
        frmGestionPrestamo.ShowDialog()
    End Sub

    Private Sub ConsultaDeCobranzaDeCuotasToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConsultaDeCobranzaDeCuotasToolStripMenuItem.Click
        Me.frmGestionCobranzaCuotas = New frmGestionCobranzaCuotas
        frmGestionCobranzaCuotas.ShowDialog()
    End Sub

    Private Sub frmPrincipal_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MessageBox.Show("¿Está seguro que desea salir de la Aplicación?", "Importante", _
        MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
            e.Cancel = False
        Else
            e.Cancel = True
        End If
    End Sub

    Private Sub frmPrincipal_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.habilitarMenu()

        Me.frmProcesos = New frmProcesos
        frmProcesos.ShowDialog()
    End Sub

    Private Sub InformesEstadísticosToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InformesEstadísticosToolStripMenuItem.Click
        Me.frmInformesEstadisticos = New frmInformesEstadisticos
        frmInformesEstadisticos.ShowDialog()
    End Sub

    Private Sub ListadoDePréstamosToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListadoDePréstamosToolStripMenuItem.Click
        Me.frmListadoPrestamos = New frmListadoPrestamos
        frmListadoPrestamos.ShowDialog()
    End Sub

    Private Sub AcercaDeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AcercaDeToolStripMenuItem.Click
        Me.frmAcercaDe = New frmAcercaDe
        frmAcercaDe.ShowDialog()
    End Sub

    Private Sub CerrarSesiónToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CerrarSesiónToolStripMenuItem.Click
        If MessageBox.Show("¿Está seguro que desea cerrar la Sesión?", "Importante", _
            MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
            Me.cerrarSesion()
        End If
    End Sub

    Private Sub IniciarSesiónToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IniciarSesiónToolStripMenuItem.Click
        Me.frmSesion = New frmSesion(Me)
        frmSesion.ShowDialog()
    End Sub

    Private Sub cerrarSesion()
        Me.sesionIniciada = False
        Me.manager = False
        Me.idUsuarioLogueado = ""
        Me.nombreUsuarioLogueado = ""
        Me.habilitarMenu()
    End Sub

    Private Sub CambiarContraseñaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CambiarContraseñaToolStripMenuItem.Click
        Me.frmPassword = New frmPassword(Me)
        frmPassword.ShowDialog()
    End Sub

    Public Sub habilitarMenu()
        For Each obj As System.Windows.Forms.ToolStripMenuItem In Me._menu.Items
            If sesionIniciada = True Then
                If obj.Name = "IniciarSesiónToolStripMenuItem" Then
                    obj.Enabled = False
                Else
                    obj.Enabled = True
                End If
            Else
                If obj.Name = "IniciarSesiónToolStripMenuItem" Then
                    obj.Enabled = True
                Else
                    obj.Enabled = False
                End If
            End If
        Next obj

        For Each obj As System.Windows.Forms.ToolStripMenuItem In Me._menu.Items
            If obj.Name = "ArchivoToolStripMenuItem" Then
                obj.Enabled = True
            End If
            If obj.Name = "ClientesToolStripMenuItem" Then
                obj.Enabled = True
            End If
            If obj.Name = "PréstamosToolStripMenuItem" Then
                obj.Enabled = True
            End If
            If obj.Name = "CobrosToolStripMenuItem" Then
                obj.Enabled = True
            End If
            If obj.Name = "EstadísticasToolStripMenuItem" Then
                obj.Enabled = True
            End If
            If obj.Name = "AdministraciónToolStripMenuItem" Then
                obj.Enabled = True
            End If
            If obj.Name = "AyudaToolStripMenuItem" Then
                obj.Enabled = True
            End If
            If obj.DropDownItems.Count > 0 Then
                habilitarSubMenu(obj.DropDownItems)
            End If
        Next obj

        If sesionIniciada = True Then
            Label1.Visible = True
            Label2.Visible = True
            Label2.Text = Me.nombreUsuarioLogueado
        Else
            Label1.Visible = False
            Label2.Visible = False
            Label2.Text = ""
        End If
    End Sub

    Private Sub habilitarSubMenu(ByRef submenu)
        For Each obj As System.Windows.Forms.ToolStripMenuItem In submenu
            If obj.Name = "AcercaDeToolStripMenuItem" Or obj.Name = "SalirToolStripMenuItem" Then
                obj.Enabled = True
            Else
                If sesionIniciada = True Then
                    If obj.Name = "IniciarSesiónToolStripMenuItem" Then
                        obj.Enabled = False
                    Else
                        If obj.Name = "GestiónDeUsuariosToolStripMenuItem" Or _
                        obj.Name = "GestiónDeMarcasDeVehículosToolStripMenuItem" Or _
                        obj.Name = "CambiarContraseñaToolStripMenuItem" Then
                            obj.Enabled = manager
                            If obj.Name = "CambiarContraseñaToolStripMenuItem" Then
                                obj.Enabled = Not manager
                            End If
                        Else
                            obj.Enabled = True
                        End If
                    End If
                Else
                    If obj.Name = "IniciarSesiónToolStripMenuItem" Then
                        obj.Enabled = True
                    Else
                        obj.Enabled = False
                    End If
                End If
            End If
        Next obj
    End Sub

End Class